# Copyright (c) 2021 Presto Labs Pte. Ltd.
# Author: qiang

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo
import json
import copy

def instrument_to_web_format(instrument_data):
  web_data = []
  keyval = {}
  for item in instrument_data:
    if item['product_type'] != "PRODUCT_TYPE_SPOT":
      continue
    keyval["symbol"] = item["symbol"]
    keyval["productType"] = item["product_type"]
    keyval["baseCurrency"] = item["base_currency"]
    keyval["quoteCurrency"] = item["quote_currency"]
    keyval["tickSize"] = item["tick_size"]
    keyval["unitOrderQty"] = item["unit_order_qty"]
    keyval["minOrderQty"] = item["min_order_qty"]
    web_data.append(copy.deepcopy(keyval))

  return web_data

def prex_pi_hand_updata():
  path = "python/coin2/support/product_info/prex/Prex_hand_update.json"
  with open(path) as fd:
    hand_data = json.load(fd)

  data = instrument_to_web_format(hand_data)
  return data

def generate():
  mea = 'Spot.Prex.v1'
  me = 'Spot.Prex'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  headers = requests.utils.default_headers()

  data = prex_pi_hand_updata()
  if (len(data) == 0):
    url = 'https://mmapi.sandbox.prexapi.com/api/v1/instrument/list'
    headers.update({'Referer': 'https://sandbox.prex.run'})
    data = requests.get(url, headers=headers).json()['instruments']

  pis = []
  for info in data:
    if info['productType'] != "PRODUCT_TYPE_SPOT":
      continue

    native_symbol = info['symbol']
    native_base = info['baseCurrency']
    native_quote = info['quoteCurrency']
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = "%s-%s" % (base, quote)

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        price_ticksize=float(info['tickSize']),
        qty_ticksize=float(info['unitOrderQty'])
    )

    # if "notionalMinOrderAmount" in info:
    #   pi["min_amount"] = float(info["notionalMinOrderAmount"])

    if "min_order_qty" in info:
      pi["min_qty"] = float(info["min_order_qty"])


    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  symbol_list = [info['native_symbol'] for info in data]
  quote_currency_list = [info['native_quote'] for info in data]
  base_currency_list = [info['native_base'] for info in data]
  currency_list = base_currency_list + quote_currency_list
  res = {
      'symbol_list': list(set(symbol_list)),
      'base_currency_list': list(set(base_currency_list)),
      'quote_currency_list': list(set(quote_currency_list)),
      'currency_list': list(set(currency_list)),
  }
  return res


if __name__ == "__main__":
  import json
  print(json.dumps(generate(), indent=2))
